
# 导入所需的库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats as stats
import warnings
from sklearn.model_selection import learning_curve
# 它用于过滤掉不需要的警告信息
warnings.filterwarnings('ignore')
# 设置字体，确保支持中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']  
plt.rcParams['axes.unicode_minus'] = False  

# 导入数据集
tarin_data = pd.read_csv('file/013.zhengqi_train.txt', sep='\t')
test_data = pd.read_csv('file/013.zhengqi_test.txt', sep='\t')

### 打印数据集的前几行
# 特征比如 包括锅炉的可调参数，如燃烧给量，一二次风，引风，返料风，给水水量；以及锅炉的工况，比如锅炉床温、床压，炉膛温度、压力，过热器的温度等
print(tarin_data.head())

## 生成所有的训练数据和测试数据分布折线图
dist_cols = 6
dist_rows = len(test_data.columns) // dist_cols + 1
plt.figure(figsize=(4*dist_cols, 4*dist_rows)) 
i=1
for col in test_data.columns:
    plt.subplot(dist_rows, dist_cols, i)
    ax = sns.kdeplot(tarin_data[col], color="Red", shade=True)
    ax = sns.kdeplot(test_data[col], color="Blue", shade=True)
    ax.set_xlabel(col, fontsize=12)
    ax.set_ylabel('概率密度', fontsize=12)
    ax = ax.legend(['训练数据', '测试数据'], loc='upper right') 
    i+=1
# 保存图片
plt.savefig("file/013.所有训练数据和测试数据分布折线图.png", dpi=300)

##  绘制线性回归模型学习曲线
def plot_learning_curve(model,title,X,y,cv=None):
    # 学习曲线计算
    train_sizes, train_scores, test_scores = learning_curve(model, X, y, cv=cv)
    # 训练数据得分和测试数据得分平均值和方差
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)
    # 训练数据得分可视化
    plt.plot(train_sizes, train_scores_mean, label="训练得分", color='r')
    plt.fill_between(train_sizes, train_scores_mean - train_scores_std, train_scores_mean +
                     train_scores_std, alpha=0.1, color='r')
    # 测试数据得分可视化
    plt.plot(train_sizes, test_scores_mean, label="测试得分", color='g')
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std, test_scores_mean+
                     test_scores_std, alpha=0.1, color='g')
    # 图画设置
    plt.grid(True, axis='y', linestyle='--')
    plt.legend(loc='lower right')
    # 绘制学习曲线
    plt.figure()
    plt.title(title)
    plt.xlabel("训练样本数")


## 筛选分布特征不符合图片
# 如下 V5,V9,V11,V17,V22,V28
cols = 3
rows = 2
plt.figure(figsize=(6*cols, 6*rows)) #设置画布宽度高度
# 循环生成图
i=1
for col in ['V5', 'V9', 'V11', 'V17', 'V22', 'V28']:
    # 设置子图 2行3列，第i+1个图
    plt.subplot(rows, cols, i)
    # 绘制训练数据分布图
    ax = sns.kdeplot(tarin_data[col], color="Red", shade=True)
    ax = sns.kdeplot(test_data[col], color="Blue", shade=True)
    # 设置X轴标签
    ax.set_xlabel(col, fontsize=12)
    # 设置Y轴标签
    ax.set_ylabel('概率密度', fontsize=12) # 出现次数频率
    # 设置标签
    ax = ax.legend(['训练数据','测试数据'], loc='upper right') # loc 位置范围
    i+=1
# 保存图片
plt.savefig("file/013.筛选分布特征不符合图片.png", dpi=300)

